﻿using System;
using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Meta;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Configuration;

namespace Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Pdf
{
    public class HierarchyCodelistModule : BaseModule
    {
        public HierarchyCodelistModule(IMetaProvider provider, Document pdfDocument, PdfFonts fontsProvider, BookConfiguration cfg)
            : base(provider, pdfDocument, fontsProvider, cfg)
        {
            ProduceAllHierarchy(metaProvider.GetRootNode());
        }

        private void ProduceAllHierarchy(HierarchyNode rootNode)
        {
            // Module title
            Tables.Add(new PdfBookPTable(TableTitle(rootNode.Name + " (" + rootNode.Id + ")" )));
            PdfPTable table = CreatePropertyTable();
            if (string.IsNullOrEmpty(rootNode.Descr) == false)
                MakeRowVarDetail(table, "Description", rootNode.Descr, false);

            // Module detail
            Tables.Add(new PdfBookPTable(table));

            // Any annotations
            CreateAnnotationTable(metaProvider.GetAnnotations(rootNode));

            // For all hierarchy
            foreach (HierarchyNode nodeHi in rootNode.Nodes)
            {
                ProduceHierarchy (nodeHi);
                doc.Add(new Paragraph("\n"));
            }
        }

        private void ProduceHierarchy(HierarchyNode rootNode)
        {
            // Module title
            Tables.Add(new PdfBookPTable(TableSubTitle(rootNode.Id + " - " + rootNode.Name)));
            PdfPTable table = null;

            // Module detail
            if (string.IsNullOrEmpty(rootNode.Descr) == false)
            {
                table = CreatePropertyTable();
                MakeRowVarDetail(table, "Description", rootNode.Descr, false);
                Tables.Add(new PdfBookPTable(table));
            }
            // Any annotations
            CreateAnnotationTable(metaProvider.GetAnnotations(rootNode));

            // For all codes
            int indexNo = 1;
            // Create list table
            table = CreateFlatListTable("Code");

            ProduceHierarchyFlatList(table, rootNode, 1, string.Empty, ref indexNo);
        }

        /*
        public void PrintPretty(HierarchyNode node, string indent, bool last)
        {
            Console.Write(indent);
            if (last)
            {
            //    Console.Write("\\-");
                indent += "  ";
            }
            else
            {
             //   Console.Write("|-");
                indent += "| ";
            }
        //    Console.WriteLine(Name);

            for (int i = 0; i < node.Nodes.Count; i++)
                PrintPretty(node.Nodes[i], indent, i == node.Nodes.Count - 1);
        }

        public void PrintNode(string prefix)
        {
            Console.WriteLine("{0} + {1} : {2}", prefix, this.Name, this.Time);
            foreach (Node n in Children)
                if (Children.IndexOf(n) == Children.Count - 1)
                    n.PrintNode(prefix + "    ");
                else
                    n.PrintNode(prefix + "   |");
        }
        */

   }
}
